package com.dodayum.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class PerformanceAspect {
    private static final Logger logger = LoggerFactory.getLogger(PerformanceAspect.class);

    @Around("execution(* com.dodayum.service..*(..))")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result;

        try {
            // 执行目标方法
            result = joinPoint.proceed();
        } catch (Throwable throwable) {
            logger.error("方法执行异常: " + joinPoint.getSignature().getName(), throwable);
            throw throwable;
        }

        long executionTime = System.currentTimeMillis() - startTime;

        // 记录方法的执行细节
        logger.info("执行方法: {}.{}()，参数: [{}]，耗时: {} ms，返回值: [{}]",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName(),
                Arrays.toString(joinPoint.getArgs()),
                executionTime,
                result);

        return result;
    }
}

